package com.bitcola.security.otc.controller;

import com.bitcola.security.otc.Vo.OtcInfo;
import com.bitcola.security.auth.client.annotation.IgnoreUserToken;
import com.bitcola.security.otc.biz.OtcOrderBiz;
import com.bitcola.security.common.context.BaseContextHandler;
import com.bitcola.security.common.msg.AppResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.stream.Collectors;

@RestController
@RequestMapping("otc")
public class OtcController {
    @Autowired
    OtcOrderBiz otcOrderBiz;

    @RequestMapping("userBuy")
    public AppResponse buy(BigDecimal number, String payWay, String merchantId) {
        if (number.compareTo(BigDecimal.TEN) >= 0) {
            var info = otcOrderBiz.userBuy(BaseContextHandler.getUserID(), number, payWay, merchantId);
            return AppResponse.ok().data(info);
        }
        return null;
    }

    @RequestMapping("bindPay")
    public AppResponse bindPay(String account, String payWay) {

        var info = otcOrderBiz.bindPayWay(BaseContextHandler.getUserID(), account, payWay);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("userPay")
    public AppResponse userPay(String orderId) {
        var info = otcOrderBiz.userPayed(BaseContextHandler.getUserID(), orderId);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("merchantPay")
    public AppResponse merchantPay(String orderId) {
        var info = otcOrderBiz.merchantPayed(BaseContextHandler.getUserID(), orderId);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("userSell")
    public AppResponse userSell(BigDecimal number, String payWay, String merchantId) {
        var info = otcOrderBiz.userSell(BaseContextHandler.getUserID(), number, merchantId, payWay);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("userCancel")
    public AppResponse userCancel(String orderId) {
        var info = otcOrderBiz.userCancel(BaseContextHandler.getUserID(), orderId);
        return AppResponse.ok().data(info);

    }


    @RequestMapping("merchantCancel")
    public AppResponse merchantCancel(String orderId) {
        var info = otcOrderBiz.merchantCancel(BaseContextHandler.getUserID(), orderId);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("merchantPending")
    public AppResponse merchantPending(String orderId) {
        var info = otcOrderBiz.merchantPending(orderId);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("merchantFinished")
    public AppResponse merchantFinished(String orderId) {

        var info = otcOrderBiz.merchantFinished(orderId);
        return AppResponse.ok().data(info);


    }

    @RequestMapping("userFinished")
    public AppResponse userFinished(String orderId) {

        var info = otcOrderBiz.userFinished(orderId);
        return AppResponse.ok().data(info);


    }

    @RequestMapping("merchantFail")
    public AppResponse merchantFail(String orderId) {
        var info = otcOrderBiz.merchantFail(orderId);
        return AppResponse.ok().data(info);

    }

    @RequestMapping("getOtcPrice")
    @IgnoreUserToken

    public AppResponse getOtcPrice() {
        var price = otcOrderBiz.price;


        return AppResponse.ok().data(price);
    }

    @RequestMapping("getOtcInfo")
    public AppResponse getOtcInfo() {
        var info = otcOrderBiz.getUserPay(BaseContextHandler.getUserID());
        return AppResponse.ok().data(info);

    }

    @RequestMapping("setPrice")
    @IgnoreUserToken
    public String setPrice(BigDecimal price) {

        otcOrderBiz.price = price;
        return "ok";

    }

    @RequestMapping("queryOrder")
    public AppResponse queryOrder(int page, int size, String status, String id,String type,String tel) {
        var userId = BaseContextHandler.getUserID();


        String finalUserId = userId;
        var merchant = otcOrderBiz.getMerchant().stream().filter(x -> x.getUserId().equals(finalUserId)).collect(Collectors.toList());
        if (merchant.size() > 0) {
            return AppResponse.ok().data(otcOrderBiz.getList(null, page, size, status, id, userId,type,tel));
        }
        return AppResponse.ok().data(otcOrderBiz.getList(userId, page, size, status, id, null,type,tel));
    }
    @RequestMapping("getMerchant")
    public AppResponse getMerchant()
    {

        return AppResponse.ok().data(otcOrderBiz.getMerchant());

    }

    @RequestMapping("getMerchantOnline")
    public AppResponse getMerchantOnline()
    {
        return AppResponse.ok().data(otcOrderBiz.getMerchantOnline(BaseContextHandler.getUserID()));

    }

    @RequestMapping("changeMerchantOnline")
    public AppResponse changeMerchantOnline()
    {
        return AppResponse.ok().data(otcOrderBiz.changeOnline(BaseContextHandler.getUserID()));


    }



}
